package com.boot.security.server.service.impl.mffl.MfflRefund;

import com.boot.security.server.dao.mffl.*;
import com.boot.security.server.model.mffl.MfflContract;
import com.boot.security.server.model.mffl.MfflRefund;
import com.boot.security.server.service.impl.mffl.WXPay.WXPayService;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MfflRefundImpl implements IMfflRefund {

    @Autowired
    private MfflRefundDao mfflRefundDao;

    @Autowired
    private MfflProfitDao mfflProfitDao;

    @Autowired
    private MfflOrderDao mfflOrderDao;

    @Autowired
    private MfflConsultationDao mfflConsultationDao;

    @Autowired
    private MfflContractDao mfflContractDao;

    @Autowired
    private WXPayService wxPayService;

    @Override
    @Transactional
    public int yesRefund(MfflRefund mfflRefund) {
        try {

            ReturnBean returnBean = wxPayService.weChatrefund(mfflRefund.getPaymerchantnumber(), null, "退款",1);
            if ("200".equals(returnBean.getErrCode())) {
//                this.mfflRefundDao.updateState(mfflRefund.getId(), 1);
                this.mfflProfitDao.updateState(mfflRefund.getOrderid(), 2);
                this.mfflOrderDao.updateState(mfflRefund.getOrderid(),2);
                if (mfflRefund.getRefundname().equals("律师函")) {
                    this.mfflConsultationDao.udpateState(mfflRefund.getPaymerchantnumber(), 2);
                } else if (mfflRefund.getRefundname().equals("合同审核")) {
                    this.mfflContractDao.udpateState(mfflRefund.getPaymerchantnumber(), 2);
                } else if (mfflRefund.getRefundname().equals("合同定制")) {
                    this.mfflContractDao.udpateState(mfflRefund.getPaymerchantnumber(), 2);
                }
                return 1;
            }else {
                throw new Exception();
            }
//
            //微信退款 ----------------
        } catch (Exception e) {
            return 0;
        }

    }

    @Override
    @Transactional
    public void noRefund(MfflRefund mfflRefund) {
        this.mfflRefundDao.updateState(mfflRefund.getId(), 3);
        this.mfflProfitDao.updateState(mfflRefund.getOrderid(), 1);
        this.mfflOrderDao.updateState(mfflRefund.getOrderid(), 1);
        if (mfflRefund.getRefundname().equals("律师函")) {
            this.mfflConsultationDao.udpateState(mfflRefund.getPaymerchantnumber(), 1);
        } else if (mfflRefund.getRefundname().equals("合同审核")) {
            this.mfflContractDao.udpateState(mfflRefund.getPaymerchantnumber(), 1);
        } else if (mfflRefund.getRefundname().equals("合同定制")) {
            this.mfflContractDao.udpateState(mfflRefund.getPaymerchantnumber(), 1);
        }
    }
}
